package www.openteam.com.adapter.policy.other;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.bean.OrderApproval;
import www.openteam.com.bean.OrderApprovalitem;
import www.openteam.com.tool.SplitXML;

/**
 * Created by kylin on 2017/4/6.
 */

public class CODetailedAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private final String TAG = CODetailedAdapter.class.getSimpleName();

    private LayoutInflater mLayoutInflater;
    private Context context;
    private StringBuffer sb = new StringBuffer();
    private OrderApproval.DataEntity orderApproval;
    private OrderApprovalitem orderApprovalitem;
    private ArrayList<OrderApprovalitem> orderApprovalitems;

    public CODetailedAdapter(Context context) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void addItems(OrderApproval.DataEntity list) {
        orderApproval = list;
        String[] prodname = orderApproval.getProdname().split(",");
        String[] prodno = orderApproval.getProdno().split(",");
        String[] senddate = orderApproval.getSenddate().split(",");
        Float[] price = SplitXML.splitStringFloat6(orderApproval.getPrice(), "price");
        Integer[] paynumb = SplitXML.splitStringInt(orderApproval.getPaynumb(), "paynumb");
        String[] oStatus = SplitXML.splitStringO(orderApproval.getoStatus(), "status");
        String[] paperinf = SplitXML.splitSignString(orderApproval.getPaperinf(), "paperinf");
        String[] CustOrderId = SplitXML.splitSignString(orderApproval.getCustOrderId2(), "CustOrderId");
        String[] CustMaterialID = SplitXML.splitSignString(orderApproval.getCustMaterialID(), "CustMaterialID");
        String[] technicsinf = SplitXML.splitSignString(orderApproval.getTechnicsinf(), "technicsinf");
//                "CustMaterialID": "<CustMaterialID>610000006084</CustMaterialID>"

        orderApprovalitems = new ArrayList<>();
        for (int i = 0; i < prodname.length; i++) {
            orderApprovalitem = new OrderApprovalitem();
            orderApprovalitem.setProdno(prodno[i]);
            orderApprovalitem.setProdname(prodname[i]);
            orderApprovalitem.setPaynumb(paynumb[i]);
            orderApprovalitem.setPrice(price[i]);
            orderApprovalitem.setCustMaterialID(CustMaterialID[i]);
            orderApprovalitem.setCustOrderId2(CustOrderId[i]);
            orderApprovalitem.setPaperinf(paperinf[i]);
            orderApprovalitem.setSenddate(senddate[i]);
            orderApprovalitem.setStatus(oStatus[i]);
            orderApprovalitem.setTechnicsinf(technicsinf[i]);
            orderApprovalitems.add(orderApprovalitem);
        }
    }


    public void addContext(Context context) {
        this.context = context;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d(TAG, "DataAdapter: 3");
        return new ViewHolder
                (mLayoutInflater.inflate(R.layout.item_base, parent, false), context);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        SharedPreferences mySetting = context.getSharedPreferences("mySetting", Context.MODE_PRIVATE);
        if (mySetting.getBoolean("ShowMoney", true)) {
            float amount = orderApprovalitems.get(position).getPaynumb() * orderApprovalitems.get(position).getPrice();
            amount = (float) (Math.round(amount * 100)) / 100;
            sb.append("产品编号:" + orderApprovalitems.get(position).getProdno()).
                    append("\n").
                    append("产品名称:" + orderApprovalitems.get(position).getProdname()).append("\n")
                    .append("客户料号:" + orderApprovalitems.get(position).getCustMaterialID()).append("\n")
                    .append("客户单号:" + orderApprovalitems.get(position).getCustOrderId2()).append("\n")
                    .append("交货日期:" + orderApprovalitems.get(position).getSenddate()).append("\n")
                    .append("用纸信息:" + orderApprovalitems.get(position).getPaperinf()).append("\n")
                    .append("工艺要求:" + orderApprovalitems.get(position).getTechnicsinf()).append("\n")
                    .append("合计:" + orderApprovalitems.get(position).getPaynumb() + " * "
                            + orderApprovalitems.get(position).getPrice() + " = ")
                    .append(amount).append("\n")
                    .append("状态:" + orderApprovalitems.get(position).getStatus()).append("\n");
        } else {
            sb.append("产品编号:" + orderApprovalitems.get(position).getProdno()).append("\n").append("产品名称:" + orderApprovalitems.get(position).getProdname()).append("\n")
                    .append("客户料号:" + orderApprovalitems.get(position).getCustMaterialID()).append("\n")
                    .append("客户单号:" + orderApprovalitems.get(position).getCustOrderId2()).append("\n")
                    .append("交货日期:" + orderApprovalitems.get(position).getSenddate()).append("\n")
                    .append("用纸信息:" + orderApprovalitems.get(position).getPaperinf()).append("\n")
                    .append("工艺要求:" + orderApprovalitems.get(position).getTechnicsinf()).append("\n")
                    .append("价格:0000 * 0000 = ").append("******").append("\n").append("状态:" + orderApprovalitems.get(position).getStatus()).append("\n");

        }
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.textView.setText("子订单:" + position + "");


        viewHolder.tv_title.setText(
                sb.toString()
        );
        sb.setLength(0);
        sb.delete(0, sb.length());

    }


    @Override
    public int getItemCount() {

        return orderApprovalitems.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private final String TAG = ViewHolder.class.getSimpleName();
        private TextView textView;
//        private TextView tv_title;
        private CardView cardView;

        @Bind(R.id.lineItem)
        LinearLayout lineItem;
        @Bind(R.id.tv_title)
        TextView tv_title;

        public ViewHolder(View itemView, Context context) {
            super(itemView);
            Log.d(TAG, "DataAdapter: 5");
            cardView = (CardView) itemView.findViewById(R.id.card);
            textView = (TextView) itemView.findViewById(R.id.tv_id);
//            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            ButterKnife.bind(this,itemView);
        }
    }

}
